package edu.rutgers.rucampusinfo.directory;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jdo.Query;

import edu.rutgers.rucampusinfo.PMF;
import edu.rutgers.rucampusinfo.tables.Employee;

import com.google.appengine.api.datastore.Blob;

public class GenerateImage extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final Logger log = Logger.getLogger(GenerateImage.class.getName());
        private int i = 0;
    @SuppressWarnings("unchecked")
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        String netid = req.getParameter("image");
        Blob image = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();
    /*
    try 
        {
            resp.setContentType("image/jpg");
            Query picQuery = pm.newQuery("SELECT FROM "+Employee.class.getName()+" WHERE netid == \""+netid+"\"");
            List<Employee> results = (List<Employee>)picQuery.execute();
            if (results.iterator().hasNext())
                {
                        for(Employee pics : results)
                        {
                                image = pics.getImage();
                                if(image!=null){
                                resp.getOutputStream().write(image.getBytes());
                                }
                                else{
                                	netid = "default";
                                picQuery = pm.newQuery("SELECT FROM "+Employee.class.getName()+" WHERE netid == \""+netid+"\"");
                                results = (List<Employee>)picQuery.execute();
                                if (results.iterator().hasNext())
                                        {
                                                for(Employee pic : results)
                                                {
                                                        image = pic.getImage();
                                                        resp.getOutputStream().write(image.getBytes());
                                                }
                                        }
                                }
                        }
                }
        }
    catch(Exception e)
        {
            e.printStackTrace();
        }
    finally
    {
        pm.close();
    } */
    }
}